package com.qy.StudentInfo.handler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qy.StudentInfo.mapper.GradeMapper;
import com.qy.StudentInfo.pojo.CoursePlan;
import com.qy.StudentInfo.pojo.Grade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.qy.StudentInfo.service.CoursePlanService;
import com.qy.StudentInfo.service.CourseService;
import com.qy.StudentInfo.service.StudentService;
import com.qy.StudentInfo.service.TeacherService;

/**
 * @date 2019年4月13日 下午5:32:39
 * 
 */
@Controller
@RequestMapping("/AjaxHandler")
public class AjaxHandler {
	@Autowired
    CourseService courseService;
	@Autowired
	StudentService studentService;
	@Autowired
	TeacherService teacherService;
	@Autowired
	CoursePlanService coursePlanService;
	@Autowired
	private GradeMapper gradeMapper;
	
	/**
	 * ajax验证课程编号是否存在
	 * @param cid
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(value="/existCid",method = RequestMethod.POST)
	public void existCid(@RequestParam("cid") String cid,HttpServletResponse response,HttpServletRequest request) throws IOException{
		System.out.println("课程编号="+cid);
 
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		System.out.println(cid+"----------");
		PrintWriter out=null;
		
		out=response.getWriter();
		if(courseService.getByCouCid(cid) != null && cid !=null && !"".equals(cid)){
			out.println("课程编号已存在");
		}else if(cid !=null && !"".equals(cid)){
			out.println("此课程编号可以使用");
		}else {
			out.println("课程编号不能为空");
		}
		out.flush();
		out.close();
		
	}
	
	@RequestMapping(value="/existSid",method = RequestMethod.POST)
	public void existSid(@RequestParam("sid") String sid,HttpServletResponse response,HttpServletRequest request) throws IOException{
		System.out.println("学号="+sid);
 
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out=null;
		
		out=response.getWriter();
		if(studentService.getByStuSid(sid) != null && sid.length() ==12){
			out.println("学号已存在");
		}else if(sid.length() ==11){
			out.println("学号可以使用");
		}else {
			out.println("学号必须是11位");
		}
		out.flush();
		out.close();
		
	}
	//ajax验证教师id
	@RequestMapping(value="/existTid",method = RequestMethod.POST)
	public void existTid(@RequestParam("tid") String tid,HttpServletResponse response,HttpServletRequest request) throws IOException{
 
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out=null;
		
		out=response.getWriter();
		if(teacherService.getByTeaTid(tid) != null && tid.length() <=5){
			out.println("教师编号已存在");
		}else if(tid.length() <=11){
			out.println("教师编号可以使用");
		}else {
			out.println("教师编号必须小于等于5位");
		}
		out.flush();
		out.close();
		
	}
	//校验授课教室是否重复
	@RequestMapping(value="/existTime",method = RequestMethod.GET)
	public void checkCoursePlan(CoursePlan coursePlan, Model model, HttpServletResponse response) throws IOException {
		System.out.println("请求参数为:courseWeek="+coursePlan.toString());
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out= response.getWriter();
		if (coursePlanService.ajaxGetCoursePlan(coursePlan.getCoursetime(), coursePlan.getCourseweek(), coursePlan.getClassroom())){
			out.flush();
			out.println("上课教室存在冲突,请检查后输入!");
		}else{
			out.println("可以正常添加课程,教室无冲突!");
		}
		out.flush();
		out.close();

	}
	/**
	 * 学生获取邮件
	 */
	@RequestMapping(value = "/endorse/{sid}/{pn}",method = RequestMethod.GET)
	public void SendMail(@PathVariable("sid") String sid, HttpServletResponse response, Grade grade) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out= response.getWriter();
		Grade allSource = gradeMapper.getAllSource(sid);
		System.out.println(allSource.toString());

	}
	
}
